<?php

namespace support;


use support\exception\Handler;
use Throwable;
use Webman\Http\Request;
use Webman\Http\Response;

class Exception extends Handler
{

    public function render(Request $request, Throwable $exception): Response
    {

        $defaultErrorText = "服务器出错，请稍后再试";
        if (in_array($exception->getCode(), [500, 999])) {  // 自定义错误信息
            $code = $exception->getCode();
            $message = empty($exception->getMessage()) ? $defaultErrorText : $exception->getMessage();
        } else {  // 框架错误信息
            $code = 500;
            $message = $defaultErrorText;
        }

        $data = [
            "code" => $code,
            "status" => false,
            "message" => $message,
            'data' => [],
            'timestamp' => time(),
        ];

        return json($data);

    }

}